/*
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 * 02111-1307, USA.
 *
 * http://www.gnu.org/copyleft/gpl.html
 */
package l1j.server.server.clientpackets;

import java.util.logging.Level;
import java.util.logging.Logger;

import l1j.server.echo.ClientThread;
import l1j.server.server.datatables.lock.CharacterConfigReading;
import l1j.server.server.model.Instance.L1PcInstance;
import l1j.server.server.templates.L1Config;

/**
 * 收到由客戶端傳來角色設定的封包
 */
public class C_CharcterConfig extends ClientBasePacket {

	private static final String C_CHARCTER_CONFIG = "[C] C_CharcterConfig";
	private static final Logger _log = Logger.getLogger(C_CharcterConfig.class.getName());
	@Override
	public String getType() {
		return C_CHARCTER_CONFIG;
	}

	@Override
	public void start(final byte[] decrypt, final ClientThread client) {
		try {
			// 資料載入
			this.read(decrypt);

			final L1PcInstance pc = client.getActiveChar();
			//System.out.println(pc);
			if (pc == null) {
				return;
			}
			final int objid = pc.getId();

			final int length = this.readD() - 3;
			final byte data[] = this.readByte();

			final L1Config config = CharacterConfigReading.get().get(objid);

			// 新建
			if (config == null) {
				CharacterConfigReading.get().storeCharacterConfig(objid, length, data);

			// 更新
			} else {
				CharacterConfigReading.get().updateCharacterConfig(objid, length, data);
			}
		} catch (final Exception e) {
			_log.log(Level.SEVERE, e.getLocalizedMessage(), e);

		} finally {
			this.over();
		}
	}
}
